/**    
 * @{#} MyDoctorAdapter.java Create on 2014-6-18 下午4:30:10    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.ui.FlowLayout;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.evan.common.adapter.BaseListAdapter;
import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.DoctorDetailActivity;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.entity.Hospital.HospitalItem;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class UserAdapter extends BaseListAdapter {

	// 显示数据是否为医生数据
	private boolean isDoctor;

	protected BitmapUtils mBitmapUtils;
	private Activity mActivity;

	/**
	 * @param activity
	 * @param array
	 */
	public UserAdapter(Activity activity, ArrayList<Object> array,
			boolean isDoctor) {
		super(activity, array);
		this.mActivity = activity;
		this.isDoctor = isDoctor;
		mBitmapUtils = new BitmapUtils(activity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.evan.common.adapter.BaseAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder viewHolder = null;
		if (CommonUtility.isNull(viewHolder)) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_doctor, null);
			viewHolder.img = (OImageView) convertView
					.findViewById(R.id.imgLayout_show);
			viewHolder.text_doctor_name = (TextView) convertView
					.findViewById(R.id.text_doctor_name);
			viewHolder.mHosLevel = (TextView) convertView
					.findViewById(R.id.treatment_hospital_level);
			viewHolder.mDoctorSkill = (FlowLayout) convertView
					.findViewById(R.id.text_doctor_skill);
			viewHolder.text_doctor_hospital = (TextView) convertView
					.findViewById(R.id.text_doctor_hospital);
			viewHolder.text_doctor_title = (TextView) convertView
					.findViewById(R.id.text_doctor_title);
			viewHolder.text_charge = (TextView) convertView
					.findViewById(R.id.text_charge);
			CommonUtility.setViewHolderTag(convertView, viewHolder);
			viewHolder.mProjectNum = (TextView) convertView
					.findViewById(R.id.text_treatment_num);
		} else {
			viewHolder = (ViewHolder) CommonUtility
					.getViewHolderTag(convertView);
		}

		JSONObject object = (JSONObject) getItem(position);

		try {
			final JSONObject doctor = object.getJSONObject("User");
			viewHolder.img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mActivity,
							DoctorDetailActivity.class);
					try {
						intent.putExtra(Constant.IDENTITY_KEY,
								doctor.toString());
						mActivity.startActivity(intent);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			viewHolder.text_doctor_name.setText(doctor.getString("Name"));
			convertView.setTag(object);

			if (isDoctor) {
				viewHolder.mDoctorSkill.setVisibility(View.VISIBLE);
				// viewHolder.mDoctorSkill.setText(CommonUtility
				// .convertJSONArray2String(
				// doctor.getJSONArray("Skilled"), " | "));
				String[] array = CommonUtility.convertJSONArray2String(
						doctor.getJSONArray("Skilled"), ",").split(",");
				for (String str : array) {
					if (str != null && !str.trim().equals("")) {
						CheckBox button = (CheckBox) LayoutInflater.from(
								mActivity).inflate(R.layout.item_filter_button,
								null);
						// button.setBackgroundResource(R.drawable.bg_frame_pressed_round);
						button.setClickable(false);
						button.setFocusable(false);
						/*
						 * button.setOnClickListener(new OnClickListener() {
						 * 
						 * @Override public void onClick(View v) {
						 * 
						 * } });
						 */
						button.setText(str);
						viewHolder.mDoctorSkill.addView(button);
					}
				}
				JSONArray professional = doctor.getJSONArray("Professional");
				User user = JsonUtil.convert(doctor.toString(), User.class);
				viewHolder.text_doctor_title.setText(CommonUtility
						.convertJSONArray2String(professional, ","));
				String hos = doctor.getString("Hospital");
				viewHolder.text_doctor_hospital.setText(hos);
				for (HospitalItem item : GlobalSettings.mHospitalItems) {
					if (user.Hospital != null && !user.Hospital.equals("")) {
						if (item.Name.equals(user.Hospital)
								|| item.FullName.equals(user.Hospital)
								|| item.Name.contains(user.Hospital)
								|| item.FullName.contains(user.Hospital)
								|| user.Hospital.contains(item.Name)
								|| user.Hospital.contains(item.FullName)) {
							if (item.Level != null && !item.Level.equals("")) {
								viewHolder.text_doctor_hospital
										.setText(item.Name);
							}
						}
					}
				}

				if (user.project_count > 0) {
					viewHolder.mProjectNum.setVisibility(View.VISIBLE);
					viewHolder.mProjectNum.setText(user.project_count
							+ "个线下治疗项目");
				} else {
					viewHolder.mProjectNum.setVisibility(View.GONE);
				}
				boolean hasName = CommonUtility.setHosLevel(user.Hospital,
						viewHolder.mHosLevel);

				if (!hasName) {
					viewHolder.mHosLevel.setVisibility(View.GONE);
				}
				StringBuilder sb = new StringBuilder();
				double cost = doctor.getDouble("Cost");
				String charge = "图文咨询  免费";
				if (cost > 0) {
					charge = cost + "";
					if (charge.endsWith(".0")) {
						charge = charge.substring(0, charge.length() - 2);
					}
					sb.append("图文咨询  ").append("￥").append(charge);
				} else {
					sb.append(charge);
				}
				String iconPath = doctor.getString("Pic");
				if (iconPath.indexOf(".") != -1) {
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath;
				} else {
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath
							+ Constant.IMG_TH;
				}
				viewHolder.text_charge.setText(sb);
				CommonUtility.displayHeadImage(viewHolder.img, mBitmapUtils,
						iconPath, convertView.getResources(),
						doctor.getInt("Sex"));
			} else {
				viewHolder.mHosLevel.setVisibility(View.INVISIBLE);
				viewHolder.text_charge.setVisibility(View.GONE);
				viewHolder.mProjectNum.setVisibility(View.GONE);
				StringBuilder sb = new StringBuilder();
				String sex = CommonUtility.sex.get(doctor.getInt("Sex"));
				String age = CommonUtility.getAgeByBirthday(doctor
						.getString("Birth"));

				sb.append(sex);
				if (!CommonUtility.isNull(age)) {
					sb.append("，");
					sb.append(age);
					sb.append("岁");
				}
				viewHolder.text_doctor_hospital.setText(sb.toString());
				viewHolder.text_doctor_hospital.setTextColor(Color.BLACK);
				// viewHolder.mDoctorSkill.setText();
			}
			String iconPath = doctor.getString("Pic");
			if (iconPath.indexOf(".") != -1) {
				iconPath = GlobalSettings.SERVER_IMG_URL + iconPath;
			} else {
				iconPath = GlobalSettings.SERVER_IMG_URL + iconPath
						+ Constant.IMG_TH;
			}
			// mBitmapUtils.display(viewHolder.img, iconPath,
			// new OBitmapLoadCallBack<View>());
			CommonUtility.displayHeadImage(viewHolder.img, mBitmapUtils,
					iconPath, convertView.getResources(), doctor.getInt("Sex"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// CommonUtility.setListViewItemBG(convertView, position);
		// viewHolder.mHosLevel

		return convertView;
	}

	class ViewHolder {
		OImageView img;
		TextView text_doctor_name, text_doctor_hospital, text_doctor_title,
				text_charge, mProjectNum;
		FlowLayout mDoctorSkill;
		TextView mHosLevel;
	}
}
